package com.cardmanager.pro.business.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cardmanager.pro.business.ConsumeTaskPOJOBusiness;
import com.cardmanager.pro.business.RepaymentTaskPOJOBusiness;
import com.cardmanager.pro.pojo.ConsumeTaskPOJO;
import com.cardmanager.pro.pojo.RepaymentTaskPOJO;
import com.cardmanager.pro.repository.RepaymentTaskPOJORepository;

import cn.jh.common.utils.DateUtil;
@Service
public class RepaymentTaskPOJOBusinessImpl implements RepaymentTaskPOJOBusiness {
	@Autowired
	private RepaymentTaskPOJORepository repaymentTaskPOJORepository;
	
	@Autowired
	private ConsumeTaskPOJOBusiness consumeTaskPOJOBusiness;
	
	@Autowired
	private EntityManager em;

	@Transactional
	@Override
	public RepaymentTaskPOJO save(RepaymentTaskPOJO repaymentTaskPOJO) {
		RepaymentTaskPOJO model = repaymentTaskPOJORepository.saveAndFlush(repaymentTaskPOJO);
		return model;
	}

	@Override
	public int queryTaskStatus0CountAndVersion(String userIdStr, String creditCardNumber, int taskStatus,String version) {
		return repaymentTaskPOJORepository.queryTaskStatusCountAndVersion(userIdStr,creditCardNumber,taskStatus,version);
	}

//	不缓存
	@Override
	public List<RepaymentTaskPOJO> findTaskType2AndTaskStatus0RepaymentTaskAndVersion(String version) {
		em.clear();
		Date nowDate = new Date();
		String nowDateTime = DateUtil.getDateStringConvert(new String(), nowDate, "yyyy-MM-dd HH:mm:ss");
		String nowDateString = DateUtil.getDateStringConvert(new String(), nowDate, "yyyy-MM-dd");
		List<RepaymentTaskPOJO> models = repaymentTaskPOJORepository.findTaskTypeAndTaskStatusRepaymentTaskAndVersion(2,0,nowDateTime,nowDateString,version);
		return models;
	}

	@Override
	public int queryTaskStatus0AndTaskType0CountAndVersion(String userId, String creditCardNumber, int taskStatus, int taskType,String version) {
		int queryTaskStatusAndTaskTypeCount = repaymentTaskPOJORepository.queryTaskStatusAndTaskTypeCountAndVersion(userId,creditCardNumber,taskStatus,taskType,version);
		return queryTaskStatusAndTaskTypeCount;
	}

	@Override
	public int queryOrderStatus1AndTaskType0CountAndVersion(String userId, String creditCardNumber, int orderStatus,int taskType,String version) {
		int queryOrderStatusAndTaskTypeCount = repaymentTaskPOJORepository.queryOrderStatusAndTaskTypeCountAndVersion(userId,creditCardNumber,orderStatus,taskType,version);
		return queryOrderStatusAndTaskTypeCount;
	}

	@Override
	@Transactional
	public RepaymentTaskPOJO[] saveArray(RepaymentTaskPOJO[] repaymentTaskPOJOs) {
		for(int i = 0;i < repaymentTaskPOJOs.length;i++){
			repaymentTaskPOJOs[i] = repaymentTaskPOJORepository.saveAndFlush(repaymentTaskPOJOs[i]);
		}
		return repaymentTaskPOJOs;
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<RepaymentTaskPOJO> findByTaskStatus0RepaymentTaskAndVersion(String userId, String creditCardNumber,String version,Pageable pageable) {
		return repaymentTaskPOJORepository.findByTaskStatusRepaymentTaskAndVersion(userId,creditCardNumber,0,version,pageable);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<RepaymentTaskPOJO> findByTaskStatus1AndOrderStatus0RepaymentTaskAndVersion(String userId, String creditCardNumber,String version,
			Pageable pageable) {
		return repaymentTaskPOJORepository.findByTaskStatusAndOrderStatusRepaymentTaskAndVersion(userId,creditCardNumber,1,0,version,pageable);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<RepaymentTaskPOJO> findByTaskStatus1AndOrderStatus1RepaymentTaskAndVersion(String userId, String creditCardNumber,String version,
			Pageable pageable) {
		return repaymentTaskPOJORepository.findByTaskStatusAndOrderStatusRepaymentTaskAndVersion(userId,creditCardNumber,1,1,version,pageable);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<RepaymentTaskPOJO> findByTaskStatus2AndOrderStatus1RepaymentTaskAndVersion(String userId, String creditCardNumber,String version,
			Pageable pageable) {
		return repaymentTaskPOJORepository.findByTaskStatusAndOrderStatusRepaymentTaskAndVersion(userId,creditCardNumber,2,1,version,pageable);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<RepaymentTaskPOJO> findByTaskStatus2AndOrderstatus0RepaymentTaskAndVersion(String userId, String creditCardNumber,String version,
			Pageable pageable) {
		return repaymentTaskPOJORepository.findByTaskStatusAndOrderStatusRepaymentTaskAndVersion(userId,creditCardNumber,2,0,version,pageable);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<RepaymentTaskPOJO> findByUserIdAndCreditCardNumberAndVersion(String userId, String creditCardNumber,String version,
			Pageable pageable) {
		return repaymentTaskPOJORepository.findByUserIdAndCreditCardNumberAndVersion(userId,creditCardNumber,version,pageable);
	}

	@Override
	public RepaymentTaskPOJO findByRepaymentTaskId(String repaymentTaskId) {
		em.clear();
		RepaymentTaskPOJO model = repaymentTaskPOJORepository.findByRepaymentTaskId(repaymentTaskId);
		if(model !=null){
			model.setCreateTime(model.getCreateTime().substring(0, model.getCreateTime().lastIndexOf(".")));
			model.setExecuteDateTime(model.getExecuteDateTime().substring(0, model.getExecuteDateTime().lastIndexOf(".")));
		}
		return model;
	}

	@Override
	public RepaymentTaskPOJO findByOrderCode(String orderCode) {
		em.clear();
		return repaymentTaskPOJORepository.findByOrderCode(orderCode);
	}

	@Transactional
	@Override
	public void updateTaskStatus4AndReturnMessageByRepaymentTaskId(String repaymentTaskId, String returnMessage) {
		em.clear();
		RepaymentTaskPOJO repaymentTaskPOJO = repaymentTaskPOJORepository.findByRepaymentTaskId(repaymentTaskId);
		if(repaymentTaskPOJO!=null){
			repaymentTaskPOJO.setRealAmount(BigDecimal.ZERO);
			repaymentTaskPOJO.setTaskStatus(4);
			repaymentTaskPOJO.setReturnMessage(returnMessage);
			repaymentTaskPOJO = repaymentTaskPOJORepository.saveAndFlush(repaymentTaskPOJO);
		}
	}

//	不缓存
	@Transactional
	@Override
	public RepaymentTaskPOJO createNewRepaymentTaskPOJO(BigDecimal amount, String userId, String creditCardNumber,int taskType, String description,String createTime,BigDecimal serviceCharge,BigDecimal rate,BigDecimal returnServiceCharge,String channelId,String channelTag,String version) {
		RepaymentTaskPOJO repaymentTaskPOJO = new RepaymentTaskPOJO();
		Date nowTime = new Date();
		Random random = new Random();
		repaymentTaskPOJO.setAmount(amount);
		repaymentTaskPOJO.setRealAmount(amount);
		repaymentTaskPOJO.setRate(rate);
		repaymentTaskPOJO.setChannelId(channelId);
		repaymentTaskPOJO.setChannelTag(channelTag);
		repaymentTaskPOJO.setVersion(version);
		repaymentTaskPOJO.setServiceCharge(serviceCharge);
		repaymentTaskPOJO.setUserId(userId);
		repaymentTaskPOJO.setReturnServiceCharge(returnServiceCharge);
		repaymentTaskPOJO.setRepaymentTaskId(DateUtil.getDateStringConvert(new String(), nowTime,"yyyyMMddHHSSS")+random.nextInt(9)+random.nextInt(9)+random.nextInt(9)+random.nextInt(9)+"1");
		repaymentTaskPOJO.setCreditCardNumber(creditCardNumber);
		repaymentTaskPOJO.setDescription(description);
		repaymentTaskPOJO.setCreateTime(createTime);
		repaymentTaskPOJO.setExecuteDate(DateUtil.getDateStringConvert(new String(), nowTime,"yyyy-MM-dd"));
		repaymentTaskPOJO.setExecuteDateTime(DateUtil.getDateStringConvert(new String(), nowTime,"yyyy-MM-dd HH:ss:mm"));
		repaymentTaskPOJO.setTaskType(taskType);
		repaymentTaskPOJO = this.save(repaymentTaskPOJO);
		return repaymentTaskPOJO;
	}

	@Override
	public RepaymentTaskPOJO findByTaskType0AndTaskStatus0RepaymentTaskPOJOAndVersion(String userId, String creditCardNumber,String version) {
		em.clear();
		return repaymentTaskPOJORepository.findByTaskTypeAndTaskStatusRepaymentTaskPOJOAndVersion(userId,creditCardNumber,0,0,version);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<Object[]> findByCreateTimeAndVersion(String userId, String creditCardNumber,String version,Pageable pageable) {
		return repaymentTaskPOJORepository.findByCreateTimeAndVersion(userId,creditCardNumber,version,pageable);
	}

	@Override
	public List<RepaymentTaskPOJO> findByCreateTimeAndVersion(String userId, String creditCardNumber, String createTime,String version) {
		em.clear();
		List<RepaymentTaskPOJO> models = repaymentTaskPOJORepository.findByCreateTimeAndVersion(userId, creditCardNumber,createTime,version);
		if(models != null && models.size() >0){
			for(RepaymentTaskPOJO model:models){
				model.setCreateTime(model.getCreateTime().substring(0,model.getCreateTime().lastIndexOf(".")));
				model.setExecuteDateTime(model.getExecuteDateTime().substring(0, model.getExecuteDateTime().lastIndexOf(".")));
			}
		}
		return models; 
	}

	@Override
	public int findByOrderStatus1AndTaskType0CountAndVersion(String userId, String creditCardNumber,String version) {
		em.clear();
		int findByOrderStatusAndTaskTypeCount = repaymentTaskPOJORepository.findByOrderStatusAndTaskTypeCountAndVersion(userId,creditCardNumber,1,0,version);
		return findByOrderStatusAndTaskTypeCount;
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<Object[]> findByCreateTimeAndTaskStatus1AndVersion(String userId, String creditCardNumber,String version,Pageable pageable) {
		return repaymentTaskPOJORepository.findByCreateTimeAndTaskStatusAndVersion(userId,creditCardNumber,1,version,pageable);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<Object[]> findByCreateTimeAndTaskStatus2AndVersion(String userId, String creditCardNumber,String version,Pageable pageable) {
		return repaymentTaskPOJORepository.findByCreateTimeAndTaskStatusAndVersion(userId,creditCardNumber,2,version,pageable);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<Object[]> findByCreateTimeAndExecuteDate0AndVersion(String userId, String creditCardNumber,String version,Pageable pageable) {
		String date = DateUtil.getDateStringConvert(new String(), new Date(), "yyyy-MM-dd");
		return repaymentTaskPOJORepository.findByCreateTimeAndExecuteDate0AndVersion(userId,creditCardNumber,date,version,pageable);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public List<Object[]> findByCreateTimeAndExecuteDate1AndVersion(String userId, String creditCardNumber,String version,Pageable pageable) {
		String date = DateUtil.getDateStringConvert(new String(), new Date(), "yyyy-MM-dd");
		return repaymentTaskPOJORepository.findByCreateTimeAndExecuteDate1AndVersion(userId, creditCardNumber,date,version,pageable);
	}

//	不缓存
	@Override
	public List<RepaymentTaskPOJO> findByExecuteDateAndVersion(String userId, String creditCardNumber,String version) {
		em.clear();
		return repaymentTaskPOJORepository.findByExecuteDateAndVersion(userId, creditCardNumber,version);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public Page<RepaymentTaskPOJO> findByUserIdAndVersion(String userId,String version,Pageable pageable) {
		return repaymentTaskPOJORepository.findByUserIdAndVersion(userId,version,pageable);
	}

	@Override
	@Cacheable(value = "RepaymentTaskPOJO",keyGenerator="keyGenerator")
	public BigDecimal findAllRealAmountByUserIdAndCreditCardNumberAndOrderStatus1AndVersion(String userId,String creditCardNumber,String version) {
		return repaymentTaskPOJORepository.findSumRealAmountByUserIdAndCreditCardNumberAndOrderStatusAndVersion(userId,creditCardNumber,1,version);
	}

	@Override
	public int queryTaskStatus1AndOrderStatus4CountAndVersion(String userId, String creditCardNumber, int taskStatus,int orderStatus,String version) {
		int queryTaskStatusAndOrderStatusCount = repaymentTaskPOJORepository.queryTaskStatusAndOrderStatusCountAndVersion( userId, creditCardNumber, taskStatus,orderStatus,version);
		return queryTaskStatusAndOrderStatusCount;
	}

	@Override
	public RepaymentTaskPOJO findByCreditCardNumberAndTaskTypeAndVersion(String creditCardNumber, int taskType,String version) {
		em.clear();
		RepaymentTaskPOJO repaymentTaskPOJO = repaymentTaskPOJORepository.findByCreditCardNumberAndTaskTypeAndVersion(creditCardNumber,taskType,version);
		return repaymentTaskPOJO;
	}

	@Override
	public int findByUserIdAndCreditCardNumberAndCreateTimeAndOrderStatus1CountAndVersion(String userId, String creditCardNumber,
			String createTime,String version) {
		return repaymentTaskPOJORepository.findByUserIdAndCreditCardNumberAndCreateTimeAndOrderStatusCountAndVersion(userId,creditCardNumber,createTime,1,version);
	}

	@Override
	public List<RepaymentTaskPOJO> findByExecuteDateAndOrderStatusAndVersion(String executeDate, int orderStatus,String version) {
		em.clear();
		return repaymentTaskPOJORepository.findByExecuteDateAndOrderStatusAndVersion(executeDate,orderStatus,version);
	}

	@Override
	public RepaymentTaskPOJO findByUserIdAndCreditCardNumberAndTaskStatusAndTaskTypeAndOrderStatusAndVersion(String userId,String creditCardNumber, int taskStatus, int taskType, int orderStatus, String version) {
		em.clear();
		return repaymentTaskPOJORepository.findByUserIdAndCreditCardNumberAndTaskStatusAndTaskTypeAndOrderStatusAndVersion(userId,creditCardNumber,taskStatus,taskType,orderStatus,version);
	}

	@Override
	public List<Object[]> findByCreateTimeAndVersion(String userId, String version, Pageable pageable) {
		em.clear();
		return repaymentTaskPOJORepository.findByCreateTimeAndVersion(userId,version,pageable);
	}

	@Override
	public List<RepaymentTaskPOJO> findByTaskTypeAndOrderStatusAndVersion(int taskType, int orderStatus,
			String version) {
		em.clear();
		return repaymentTaskPOJORepository.findByTaskTypeAndOrderStatusAndVersion(taskType,orderStatus,version);
	}

	@Transactional
	@Override
	public void saveReapymentTaskAndConsumeTask(RepaymentTaskPOJO repaymentTaskPOJO, ConsumeTaskPOJO consumeTaskPOJO) {
		repaymentTaskPOJORepository.saveAndFlush(repaymentTaskPOJO);
		consumeTaskPOJOBusiness.save(consumeTaskPOJO);
	}
	
	@Transactional
	@Override
	public RepaymentTaskPOJO updateTaskStatusAndOrderStatusAndReturnMessageByRepaymentTaskId(int taskStatus,int orderStatus, String returnMessage, String repaymentTaskId) {
		RepaymentTaskPOJO repaymentTaskPOJO = this.findByRepaymentTaskId(repaymentTaskId);
		if (repaymentTaskPOJO != null && repaymentTaskPOJO.getOrderStatus().intValue() != 1) {
			repaymentTaskPOJO.setTaskStatus(taskStatus);
			repaymentTaskPOJO.setOrderStatus(orderStatus);
			repaymentTaskPOJO.setReturnMessage(returnMessage);
			repaymentTaskPOJO = this.save(repaymentTaskPOJO);
		}
		return repaymentTaskPOJO;
	}

	@Override
	public List<RepaymentTaskPOJO> findByTaskTypeAndTaskStatusAndVersionAndExecuteDate(int taskType, int taskStatus,
			String version, String e) {
		return repaymentTaskPOJORepository.findByTaskTypeAndTaskStatusAndVersionAndExecuteDate(taskType, taskStatus,version,e);
	}

}
